#include "framewidget.h"
#include "clickableframe.h"

#include <QtGui>

FrameWidget::FrameWidget(QWidget *parent) : QWidget(parent)
{
    m_frameHash = new QHash<QString, ClickableFrame*>();
    m_layout = new QVBoxLayout(this);
    this->setLayout(m_layout);


    //this->resize(this->minimumSizeHint());
}

void FrameWidget::addFrame(QString filename)
{
    qDebug() << "adding" << filename;
    //QPixmap pixmap(filename);
    ClickableFrame *frame = new ClickableFrame(filename, this);
    //button->setIconSize(pixmap.size());
    //button->setFixedSize(pixmap.size());
    //button->setFlat(true);
    //button->setCheckable(true);
    //button->resize(pixmap.size());

    m_frameHash->insert(filename, frame);


    m_layout->addWidget(frame);

    connect(frame, SIGNAL(clicked(QString)), this, SLOT(frameClicked(QString)));
}

void FrameWidget::removeFrame(QString filename)
{
    ClickableFrame *frame = m_frameHash->take(filename);
    m_layout->removeWidget(frame);
    delete frame;
}

void FrameWidget::clear()
{
    QList<QString> keys = m_frameHash->keys();

    QString key;
    foreach (key, keys)
    {
        removeFrame(key);
    }
}

void FrameWidget::frameClicked(QString filename)
{
    QHashIterator<QString, ClickableFrame*> i(*m_frameHash);

    while (i.hasNext())
    {
        i.next();

        if (i.key() == filename)
        {
            i.value()->setActive(true);
        }
        else
        {
            i.value()->setActive(false);
        }
    }

    emit clicked(filename);
}

QSize FrameWidget::minimumSizeHint() const
{
    return QSize(100, 100);
}
